﻿/*****************************
 * Author: YanNI
 * Create：2018/3/7 14:27:55
 * Email:  admin@ircove.com
 * QQ:     401608886
 * TEl: 13520446271
 ****************************/
using System;
using System.Configuration;
using System.Net.Sockets;
using System.Threading;

namespace VFPush
{
    public class Class1
    {
        static void Connect(String server, String message)
        {
            try
            {
                // Create a TcpClient.
                // Note, for this client to work you need to have a TcpServer 
                // connected to the same address as specified by the server, port
                // combination.
                Int32 port = 13000;
                TcpClient client = new TcpClient(server, port);

                // Translate the passed message into ASCII and store it as a Byte array.
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

                // Get a client stream for reading and writing.
                //  Stream stream = client.GetStream();

                NetworkStream stream = client.GetStream();

                // Send the message to the connected TcpServer. 
                stream.Write(data, 0, data.Length);

                Console.WriteLine("Sent: {0}", message);

                // Receive the TcpServer.response.

                // Buffer to store the response bytes.
                data = new Byte[256];

                // String to store the response ASCII representation.
                String responseData = String.Empty;

                // Read the first batch of the TcpServer response bytes.
                Int32 bytes = stream.Read(data, 0, data.Length);
                responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                Console.WriteLine("Received: {0}", responseData);

                // Close everything.
                stream.Close();
                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }

            Console.WriteLine("\n Press Enter to continue...");
            Console.Read();
        }

        public static void Main()
        {

            
            Console.WriteLine("ok");

            string server = ConfigurationManager.AppSettings["vfpush_service"];
            int port = Convert.ToInt32(ConfigurationManager.AppSettings["vfpush_port"]);
            TcpClient client = new TcpClient();
            client.NoDelay = true;
            client.SendTimeout = 60 * 1000;

            client.Connect(server, port);

            NetworkStream stream = client.GetStream();
   
            Thread thread = new Thread(x =>
            {

                  int i = 100000;
                  byte[] btyes= new byte[1];

                  byte b = 65;
                  btyes[0] = b;
                  while (i>0)
                  {
                      i--;
                      try
                      {
                          stream.Write(btyes, 0, 1);
                          //Console.WriteLine(btyes[0]);
                      }
                      catch(Exception ex)
                      {
                          Console.WriteLine(ex);
                      }
                      
                  }
                  Console.WriteLine("Rok");
            });

            Thread thread1 = new Thread(x =>
            {

                byte[] btyes = new byte[1];

                int k = 100000;
                while (k>0)
                {

                    k--;
                    try
                    {
                        stream.Read(btyes, 0, 1);

                        Console.WriteLine(btyes[0]);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }

                }
                Console.WriteLine("wok");
            });
            
            thread.Start();
            thread1.Start();
            
            Console.Read();
        }
    }
}
